package com.sgb.member.view_model;

import com.sgb.member.MemberConfig;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.ClientQualificationBean;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.ui.activity.ClientQualificationActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * Created by：冯齐 on 2021/1/26 16:54
 * Description：
 */
public class ClientQualificationModel extends BaseViewModel {

    ClientQualificationActivity activity;
    MemberRepertory repertory;

    private int current;

    public ClientQualificationModel(ClientQualificationActivity activity, MemberRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void getQualifications(boolean isLoadMore, String searchTxt, String no1, String no3, String no31, String no4, String no5, String no6) {
        int currentPage = isLoadMore ? current + 1 : 1;
        Map<String, Object> map = new HashMap<>();
        map.put("manageType", no1); //经营类型
        map.put("level", "");  //会员等级
        map.put("type", no3);   //资质类型
        map.put("qualificationsLevel", no31); //资质等级
        map.put("provinceNo", no4);
        map.put("cityNo", no5);
        map.put("recordUnitNo", no6); //备案单位号
        map.put("search", searchTxt); // 搜索名称和业绩名称
        map.put("limit", 20);
        map.put("page", currentPage);
        DialogHelper.showProgressDialog(activity, "数据获取中...").setCanceledOnTouchOutside(false);
        repertory.getApi().getMemberQualifications(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<ClientQualificationBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<ClientQualificationBean>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                activity.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<ClientQualificationBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                activity.showEmpty();
            }
        });
    }

    // 获取经营类型
    public void getServiceTypeList(boolean isShowPopup) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getApi().getServiceTypeList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                activity.setServiceTypeList(isShowPopup, data.getData());
            }
        });
    }

    // 资质类型
    public void getQualifyTypeTreeList(boolean isShowPopup) {
        DialogHelper.showProgressDialog(activity, "数据获取中...").setCanceledOnTouchOutside(false);
        repertory.getApi().getQualifyTypeTreeList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                activity.setQualifyTypes(isShowPopup, data.getData());
            }
        });
    }

    /**
     * 获取省市区地址
     */
    public void getAddressModel(boolean isShowPopup) {
        DialogHelper.showProgressDialog(activity, "数据获取中...").setCanceledOnTouchOutside(false);
        repertory.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    List<CommonPopEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> datum : data.getData()) {
                        entities.add(new CommonPopEntity(datum.getNo(), datum.getSimpleName()));
                    }
                    activity.setAddressData(isShowPopup, data.getData(), entities);
                }
            }
        });
    }

    public void getAddressCityData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData, String no, int type, boolean isShowLeftDialog) {
        for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> scrDatum : scrData) {
            if (scrDatum.getId().equals(no)) {
                List<AddressEntity<List<AddressEntity>>> children = scrDatum.getChildren();
                if (children != null && children.size() > 0) {
                    List<CommonPopEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity>> child : children) {
                        entities.add(new CommonPopEntity(child.getNo(), child.getSimpleName()));
                    }
                    activity.setAddressCityData(isShowLeftDialog, entities);
                }
            }
        }
    }

    // 资质类型
    public void getFindDepartment() {
        DialogHelper.showProgressDialog(activity, "数据获取中...").setCanceledOnTouchOutside(false);
        repertory.getApi().getFindDepartment().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                activity.getFindDepartment(data.getData());
            }
        });
    }

    // 资质类型
    public void getDepartmentChoose(List<CommonPopEntity> list, String no, boolean isShowLeftDialog) {
        if(list == null || list.size() == 0){
            return;
        }
        for (CommonPopEntity entity : list) {
            if(entity.getNo().equals(no)){
                activity.setFindUnitByProvinceNo(isShowLeftDialog, entity.getChildren());
                return;
            }
        }
    }


    public void getUrlSetting(String param, String id) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getApi().getUrlSetting(param).enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                String url = data.getData().getUrl() + "?id=" + id;
                MemberConfig.getInstance().startWebViewX5(activity, url, 101);
            }
        });
    }

}
